package com.llb666.shop.items.infrastructure.persistence;

import com.llb666.shop.items.domain.entity.Category;
import com.llb666.shop.items.domain.repository.CategoryRepository;
import com.llb666.shop.items.infrastructure.conver.CategoryCvt;
import com.llb666.shop.items.infrastructure.mapper.CategoryMapper;
import com.llb666.shop.items.infrastructure.po.CategoryPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * 商品分类仓储层实现类.
 *
 * @author llb66
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class CategoryRepositoryImpl implements CategoryRepository {

    private final CategoryMapper categoryMapper;

    /**
     * 查询最上级分类列表.
     *
     * @return 商品分类列表
     */
    @Override
    public List<Category> queryAllCategoryRoot() {
        Example example = new Example(CategoryPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", 1);
        return CategoryCvt.INSTANCE.po2doList(categoryMapper.selectByExample(example));
    }

    /**
     * 查询二级商品类目.
     *
     * @param rootCatId 父级商品类目Id,即以及商品类目.
     * @return 二级商品类目列表
     */
    @Override
    public List<Category> getCategoryByRootCatId(Integer rootCatId) {
        Example example = new Example(CategoryPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("fatherId", rootCatId);
        return CategoryCvt.INSTANCE.po2doList(categoryMapper.selectByExample(example));
    }

    /**
     * 根据父级商品类目id列表,查询子级商品类目列表.
     *
     * @param rootCatIds 父级商品类目Id列表.
     * @return 商品类目列表
     */
    @Override
    public List<Category> getCategoryByRootCatIds(Set<Integer> rootCatIds) {
        Example example = new Example(CategoryPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("fatherId", rootCatIds);
        return CategoryCvt.INSTANCE.po2doList(categoryMapper.selectByExample(example));
    }

    /**
     * 根据商品类型id查询商品类目.
     *
     * @param rootCatIds 商品类目id列表
     * @return 商品类目
     */
    @Override
    public List<Category> getCategoryByRootId(List<Integer> rootCatIds) {
        Example example = new Example(CategoryPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", rootCatIds);
        return CategoryCvt.INSTANCE.po2doList(categoryMapper.selectByExample(example));
    }

}
